package com.hy.common;

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import java.util.Hashtable;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: hy
 * @Date: Created in 2019/9/23/15:13
 * @Description: 网络请求工具类
 * @Version: 1.0
 */
public class HttpUtils {

    private static final String[] UA_ARRAY = {
            "Mozilla/5.0 (Windows; U; Windows NT 6.1; rv:2.2) Gecko/20110201"
            , "Mozilla/5.0 (Windows; U; Windows NT 6.1; it; rv:2.0b4) Gecko/20100818"
            , "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9a3pre) Gecko/20070330"
            , "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.9.2a1pre) Gecko"
            , "Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9.2.3) Gecko/20100401 Lightningquail/3.6.3"
            , "Mozilla/5.0 (X11; ; Linux i686; rv:1.9.2.20) Gecko/20110805"
            , "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.2.13) Gecko/20101203 iPhone"
            , "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.13; ) Gecko/20101203"
            , "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1b3) Gecko/20090305"
            , "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.9.0.9) Gecko/2009040821"
            , "Mozilla/5.0 (X11; U; Linux i686; ru; rv:1.9.0.8) Gecko/2009032711"
            , "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.7) Gecko/2009032803"
            , "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.0.7) Gecko/2009021910 MEGAUPLOAD 1.0"
            , "Mozilla/5.0 (Windows; U; BeOS; en-US; rv:1.9.0.7) Gecko/2009021910"
            , "Mozilla/5.0 (X11; U; Linux i686; pl-PL; rv:1.9.0.6) Gecko/2009020911"
            , "Mozilla/5.0 (X11; U; Linux i686; en; rv:1.9.0.6) Gecko/20080528"
            , "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.6) Gecko/2009020409"
            , "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.3) Gecko/2008092814 (Debian-3.0.1-1)"
            , "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008092816"
            , "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.3) Gecko/2008090713"
            , "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.2) Gecko Fedora/1.9.0.2-1.fc9"
            , "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.14) Gecko/2009091010"
            , "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.10) Gecko/2009042523"
            , "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.0.1) Gecko/2008072610"
            , "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008072820 Ubuntu/8.04 (hardy) (Linux Mint)"
            , "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko"
            , "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.1) Gecko/2008070206"
            , "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-au; rv:1.9.0.1) Gecko/2008070206"
            , "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko"
            , "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko"
            , "Mozilla/5.0 (Windows; U; Windows NT 5.1; cs; rv:1.9) Gecko/2008052906"
            , "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8b2) Gecko/20050702"
            , "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8b) Gecko/20050217"
            , "Mozilla/5.0 (Windows; U; Windows NT 5.1; de-AT; rv:1.8b) Gecko/20050217"
            , "Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8b) Gecko/20050217"
            , "Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.8a6) Gecko/20050111"
            , "Mozilla/5.0 (Windows; U; Windows NT 5.1; de-AT; rv:1.8a5) Gecko/20041122"
            , "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8a4) Gecko/20040927"
            , "Mozilla/5.0 (Windows; U; Windows NT 5.0; de-AT; rv:1.8a4) Gecko/20040927"
            , "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8a3) Gecko/20040817"
            , "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8a1) Gecko/20040520"
            , "Mozilla/5.0 (Windows; U; Windows NT 5.1; de-AT; rv:1.8a1) Gecko/20040520"
            , "Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.1a2) Gecko/20060512"
            , "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.8) Gecko/20071022"
            , "Mozilla/5.001 (X11; U; Linux i686; rv:1.8.1.6; de-ch) Gecko/25250101 (ubuntu-feisty)"
            , "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.4) Gecko/20070531"

    };


    /**
     * 获取一个JsoupConnection对象
     *
     * @param url
     * @return
     */
    public static Connection getJsoupConnection(String url) {

        String ua = HttpUtils.UA_ARRAY[(new Random().nextInt(HttpUtils.UA_ARRAY.length - 1))];

        System.out.println(LogUtils.getCurrentMethodName() + "：" + ua);

        return Jsoup.connect(url)
                //Jsoup默认可获取最大数据值是1mb，设置为0则不限制
                //https://www.oschina.net/question/3983466_2311284
                .maxBodySize(0)
                //忽略ContextType的检查
                .ignoreContentType(true)
                //设置UA
                .userAgent(ua)
//                //设置头信息
//                .headers(HttpUtils.createHeadersMap(s))
                .timeout(500000);
    }


    /**
     * 将String的Headers信息转换为Map
     *
     * @param headersStr 头部信息
     * @return HashMap的Headers信息
     */
    public static Map<String, String> createHeadersMap(String headersStr) {

        String pattern = "([-a-zA-Z]+): ([^0-9].*)";

        Matcher m = Pattern.compile(pattern).matcher(headersStr.trim());

        Map<String, String> headersMap = new Hashtable<>();

        while (m.find()) {

            //分割字符 格式为>>Host: free-ss.site
            String[] split = m.group().split(": ");

            //将分割的字符存入Map
            headersMap.put(split[0], split[1]);
        }

        return headersMap;
    }
}
